#include "modulehplctimeprogram.h"
#include  <QDebug>
#include "modulehplctimeprogramconfigiddefine.h"
#include "modulehplctimeprogramparamiddefine.h"
#include "modulehplcdefine.h"
#include "modulehplccmdid.h"

ModuleHplcTimeProgram::ModuleHplcTimeProgram(QObject *parent)
    : BaseModule (parent)
{
    m_timer = new QTimer(this);
    m_timer->setInterval(500); // BASE_TICK_MS
    connect(m_timer, &QTimer::timeout, this, &ModuleHplcTimeProgram::onTimeout);

    m_hplcTimeProgram = new HplcTimeProgram(this);
    m_hplcTimeProgram->setTickPeriod((double)m_timer->interval() / 1000.0);
    connect(m_hplcTimeProgram, &HplcTimeProgram::injectProgramFinished, this, &ModuleHplcTimeProgram::injectProgramFinished);
    connect(m_hplcTimeProgram, &HplcTimeProgram::rowChanged, this, &ModuleHplcTimeProgram::rowChanged);
    connect(m_hplcTimeProgram, &HplcTimeProgram::loopTimesChanged, this, &ModuleHplcTimeProgram::loopTimesChanged);
    connect(m_hplcTimeProgram, &HplcTimeProgram::timeProgramFinished, this, &ModuleHplcTimeProgram::timeProgramFinished);
    connect(m_hplcTimeProgram, &HplcTimeProgram::sampleTimeout, this, &ModuleHplcTimeProgram::sampleTimeout);
    connect(m_hplcTimeProgram, &HplcTimeProgram::totalRunTimeout, this, &ModuleHplcTimeProgram::totalRunTimeout);
    connect(m_hplcTimeProgram, &HplcTimeProgram::statusChanged, this, &ModuleHplcTimeProgram::statusChanged);
}

ModuleHplcTimeProgram::~ModuleHplcTimeProgram()
{
    delete  m_timer;
}

bool ModuleHplcTimeProgram::setConfig(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcTimeProgramConfigID_Start:
            m_hplcTimeProgram->start();
            return true;
        case ModuleHplcTimeProgramConfigID_StartFirstLine:
            m_hplcTimeProgram->startFirstRow();
            return true;
        case ModuleHplcTimeProgramConfigID_StartBasicParam:
            m_hplcTimeProgram->startBasicParam();
            return true;
        case ModuleHplcTimeProgramConfigID_Pause:
            m_hplcTimeProgram->pause();
            return true;
        case ModuleHplcTimeProgramConfigID_Restore:
            m_hplcTimeProgram->restore();
            return true;
        case ModuleHplcTimeProgramConfigID_Stop:
            m_hplcTimeProgram->stop();
            return true;
        case ModuleHplcTimeProgramConfigID_StartStopAllPumps:
            m_hplcTimeProgram->startStopAllPump(var.toBool());
            return true;

        case ModuleHplcTimeProgramConfigID_StartInjectProgram:
            m_hplcTimeProgram->startInjectProgram();
            return true;
        default:
            return false;
    }
}

bool ModuleHplcTimeProgram::getConfig(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcTimeProgramConfigID_Start:
            {
            bool isStart = m_hplcTimeProgram->isRunning();
            var.setValue(isStart);
            }
            return true;
        case ModuleHplcTimeProgramConfigID_StartFirstLine:
            return true;
        case ModuleHplcTimeProgramConfigID_StartBasicParam:
            return true;
        case ModuleHplcTimeProgramConfigID_Pause:
            {
            bool isPause = m_hplcTimeProgram->isPause();
            var.setValue(isPause);
            }
            return true;
        case ModuleHplcTimeProgramConfigID_Restore:
            return true;
        case ModuleHplcTimeProgramConfigID_Stop:
            return true;
        case ModuleHplcTimeProgramConfigID_StartStopAllPumps:
            return true;
        default:
            return false;
    }
}

bool ModuleHplcTimeProgram::setParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcTimeProgramParamID_SoftwareVersion:
            return true;
        case ModuleHplcTimeProgramParamID_HardwareVersion:
            return true;
        case ModuleHplcTimeProgramParamID_TimeProgram:
            {
                QList<TimeProgramUnit> programs;
                programs = var.value<QList<TimeProgramUnit>>();
                m_hplcTimeProgram->setProgram(programs);
            }
            return true;
        case ModuleHplcTimeProgramParamID_LoopTimes:
            {
                int times = var.value<int>();
                m_hplcTimeProgram->setLoopTimes(times);
            }
            return true;
        case ModuleHplcTimeProgramParamID_UsedTimeProgram:
            {
                bool isUsed = var.value<bool>();
                m_hplcTimeProgram->setUsedTimeProgram(isUsed);
            }
            return true;
        case ModuleHplcTimeProgramParamID_SampleTime:
            {
                double time = var.value<double>();
                m_hplcTimeProgram->setSampleTime(time);
            }
            return true;
        case ModuleHplcTimeProgramParamID_TotalRunTime:
            {
                double time = var.value<double>();
                m_hplcTimeProgram->setTotalRunTime(time);
            }
            return true;
        case ModuleHplcTimeProgramParamID_InjectFlow:
            {
                float flow = var.value<float>();
                m_hplcTimeProgram->setInjectFlow(flow);
            }
            return true;
        case ModuleHplcTimeProgramParamID_InjectTime:
            {
                double time = var.value<double>();
                m_hplcTimeProgram->setInjectTime(time);
            }
            return true;
        default:
            return false;
    }
}

bool ModuleHplcTimeProgram::getParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcTimeProgramParamID_SoftwareVersion:
            return true;
        case ModuleHplcTimeProgramParamID_HardwareVersion:
            return true;
        case ModuleHplcTimeProgramParamID_TimeProgram:
            {
            QList<TimeProgramUnit> programs = m_hplcTimeProgram->getProgram();
            var.setValue(programs);
            }
            return true;
        case ModuleHplcTimeProgramParamID_LoopTimes:
            {
            int times = m_hplcTimeProgram->getLoopTimes();
            var.setValue(times);
            }
            return true;
        case ModuleHplcTimeProgramParamID_UsedTimeProgram:
            {
            bool isUsed = m_hplcTimeProgram->getUsedTimeProgram();
            var.setValue(isUsed);
            }
            return true;
        case ModuleHplcTimeProgramParamID_SampleTime:
            {
            double time = m_hplcTimeProgram->getSampleTime();
            var.setValue(time);
            }
            return true;
        case ModuleHplcTimeProgramParamID_TotalRunTime:
            {
            double time = m_hplcTimeProgram->getTotalRunTime();
            var.setValue(time);
            }
            return true;
        case ModuleHplcTimeProgramParamID_InjectFlow:
            {
            float flow = m_hplcTimeProgram->getInjectFlow();
            var.setValue(flow);
            }
            return true;
        case ModuleHplcTimeProgramParamID_InjectTime:
            {
            double time = m_hplcTimeProgram->getInjectTime();
            var.setValue(time);
            }
            return true;
        default:
            return false;
    }
}

void ModuleHplcTimeProgram::onTimeout()
{
    QMutexLocker locker(m_mutex);
    m_hplcTimeProgram->onTick();
}

void ModuleHplcTimeProgram::onStart()
{
    QMutexLocker locker(m_mutex);
    if (m_enable)
        m_timer->start();
}
